import React, { Component } from "react";
import axios from "axios"
import Button from "antd/lib/button";
import message from "antd/lib/message";
import {
  LOGIN_LINK,
  USER_INFO_LINK,
  MY_LINK
} from "../actions/types";
class CompetitionApply extends Component {
  state={date:"",location:"",match_name:""}
  componentDidMount(){
    axios.get("/Tgh/data/for_apply").then(res=>{
      this.setState({
        date:res.data.date,
        location:res.data.location,
        match_name:res.data.match_name,
      })
    })
  }
  render(){
    return(
      <div>
        <div className="top-title">
          <i className="fa fa-angle-left back" onClick={()=>this.props.history.go(-1)}/>
          报名参赛
        </div>
        <div className="competition-bg">
          {this.renderDiffDesc()}
        </div>
      </div>
    )
  }
  renderDiffDesc(){
    const { date, location, match_name } = this.state;
    if(match_name !== ""){
      return(
        <div className="competition-desc">
          <div>{match_name}</div>
          <div>{location}</div>
          <div>{date}</div>
          <Button type="primary" style={{fontSize:16}} ghost={true} onClick={()=>{this.applyMatch()}}>报名</Button>
        </div>
      )
    }else{
      return(
        <div className="competition-desc">
          <div>暂无比赛</div>
        </div>
      )
    }
  }
  applyMatch(){
    axios.get(`/Tgh/data/apply?match_name=${this.state.match_name}`).then(res=>{
      // console.log(res.data);
      if(res.data.status === "error"){
        message.warning("用户未登录，请先登陆")
        this.props.history.replace(`${LOGIN_LINK}`);
        return;
      }
      if(res.data.status === "false1"){
        message.warning("已成功报名，请勿重复报名");
        this.props.history.replace(`${MY_LINK}`);
        return;
      }
      if(res.data.status === "false2"){
        message.warning("请先完善个人资料");
        this.props.history.replace(`${USER_INFO_LINK}`);
        return;
      }
      message.success("报名成功");
      this.props.history.replace(`${MY_LINK}`);
    })
  }
}

export default CompetitionApply